package com.zhongyouyun.lbyc.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.zhongyouyun.admin.model.UserModel;
import com.zhongyouyun.lbyc.excel.CustomBaseExcel;
import com.zhongyouyun.lbyc.excel.CustomModernBrandWidthExcel;
import com.zhongyouyun.lbyc.model.CustomBaseModel;
import com.zhongyouyun.lbyc.model.CustomModernBrandWidthModel;
import com.zhongyouyun.lbyc.mapper.CustomModernBrandWidthMapper;
import com.zhongyouyun.lbyc.pojo.CustomModernBrandWidthPojo;
import com.zhongyouyun.lbyc.service.CustomModernBrandWidthService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.other.exception.BizException;
import com.zhongyouyun.utils.ExcelUtils;
import com.zhongyouyun.utils.ModelUtils;
import com.zhongyouyun.utils.QueryUtils;
import com.zhongyouyun.utils.StrUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Author: shenyi
 * @Date: 2023-11-14
 * @Description: 服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CustomModernBrandWidthServiceImpl extends ServiceImpl<CustomModernBrandWidthMapper, CustomModernBrandWidthModel> implements CustomModernBrandWidthService {
    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 分页查询
     * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public IPage<CustomModernBrandWidthModel> selectPage(Page<CustomModernBrandWidthModel> page, QueryWrapper<CustomModernBrandWidthModel> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }


    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<CustomModernBrandWidthModel> selectList(QueryWrapper<CustomModernBrandWidthModel> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<CustomModernBrandWidthPojo> selectPojoPage(Page page, QueryWrapper<CustomModernBrandWidthPojo> queryWrapper) {
        return this.baseMapper.selectPojoPage(page, queryWrapper);
    }

    @Override
    @Transactional
    public Boolean export(MultipartFile file, String quarter) {
        Boolean flag = false;
        UserModel model = (UserModel) SecurityUtils.getSubject().getPrincipal(); // 获取当前登录用户
        List<CustomModernBrandWidthModel> customModernBrandWidthModelList = CollUtil.newArrayList();
        try {
            long stime = System.currentTimeMillis();
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.in("dept_id", QueryUtils.findChaDeptIds(model));
            queryWrapper.eq("quarter",quarter);
            remove(queryWrapper);
            InputStream inputStream = file.getInputStream();
            ModelUtils modelutils = new ModelUtils();
            List<List<String>> list = ExcelUtils.readExcelByInputStream(file.getOriginalFilename(), inputStream, 4,0);
            for (List<String> row : list) {
                System.out.println(row.get(1).trim());
               if (ObjectUtil.isNotEmpty(row.get(1).trim())&&ObjectUtil.isNotEmpty(row.get(3).trim())) {
                   CustomModernBrandWidthModel customModernBrandWidthModel = new CustomModernBrandWidthModel();
                   customModernBrandWidthModel.setCustomCode(row.get(1).trim());
                   customModernBrandWidthModel.setQuarter(quarter);
                   customModernBrandWidthModel.setGearPosition(row.get(3).trim());
                   customModernBrandWidthModel.setBcSalesVolume(new BigDecimal(StrUtils.chuzif(row.get(4))).setScale(2, BigDecimal.ROUND_UP));
                   customModernBrandWidthModel.setBcSalesAmount(new BigDecimal(StrUtils.chuzif(row.get(5))).setScale(2, BigDecimal.ROUND_UP));
                   customModernBrandWidthModel.setBcSbrandWidth(new BigDecimal(StrUtils.chuzif(row.get(6))).setScale(2, BigDecimal.ROUND_UP));
                   customModernBrandWidthModel.setCSalesVolume(new BigDecimal(StrUtils.chuzif(row.get(7))).setScale(2, BigDecimal.ROUND_UP));
                   customModernBrandWidthModel.setCSbrandWidth(new BigDecimal(StrUtils.chuzif(row.get(8))).setScale(2, BigDecimal.ROUND_UP));
                   customModernBrandWidthModel.setSbrandWidthProportion(new BigDecimal(StrUtils.chuzif(row.get(9))).setScale(2, BigDecimal.ROUND_UP));
                   customModernBrandWidthModel.setMarketType(row.get(10).trim());
                   customModernBrandWidthModel.setDeptId(model.getDeptId());
                   modelutils.initModel(customModernBrandWidthModel, model.getId().toString());
                   customModernBrandWidthModelList.add(customModernBrandWidthModel);
               }
            }
            if (customModernBrandWidthModelList.size() > 0) {
                saveBatch(customModernBrandWidthModelList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException(1, "导入错误", false);
        }
        return flag;
    }
}
